package main.java.design.iterator;

import java.util.List;

/**
 * <p></p>
 *
 * @author : wangdejian
 * @version 1.0
 * @date : 2020/6/21 16:50
 */
public class ConcreteIterator<T> implements Iterator<T> {

    List<T> list;

    //定义一个游标，用于记录当前访问位置
    private int cursor;

    public ConcreteIterator(List<T> list) {
        this.list = list;
    }

    @Override
    public T first() {
        return list.get(0);
    }

    @Override
    public T last() {
        return list.get(list.size() - 1);
    }

    @Override
    public T next() {
        T t = null;

        if (this.hasNext()) {
            t = list.get(cursor++);
        }
        return t;
    }

    @Override
    public boolean hasNext() {
        if (cursor < list.size()) {
            return true;
        }
        return false;
    }

    @Override
    public T currentItem() {
        return list.get(cursor);
    }
}
